package com.cykj.service.Impl;

import com.cykj.dto.ResponseDTO;
import com.cykj.mapper.GoodsMapper;
import com.cykj.pojo.GoodsDo;
import com.cykj.pojo.GoodsSpecificationDo;
import com.cykj.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: jd-project
 * @BelongsPackage: com.cykj.service.Impl
 * @Author: 小林
 * @CreateTime: 2025-02-25 20:31
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper mapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public ResponseDTO getHostGoodsToRedis() {
        List<GoodsDo> hostGoodsToRedis = mapper.getHostGoodsToRedis();

        redisTemplate.opsForValue().set("hostGoods", hostGoodsToRedis);

        for (GoodsDo hostGoodsToRedi : hostGoodsToRedis) {
            if (!hostGoodsToRedi.getGoodsSpecifications().isEmpty()) {
                for (GoodsSpecificationDo goodsSpecification : hostGoodsToRedi.getGoodsSpecifications()) {
                    redisTemplate.opsForValue().set("hostGoods:" + hostGoodsToRedi.getGoodsCategory() + ":"
                            + goodsSpecification.getOptionOne() + ":" + goodsSpecification.getOptionTwo(), goodsSpecification);
                }
            }
        }
        return null;
    }

    @Override
    public ResponseDTO getOption(String category, String optionOne, String optionTwo) {
        Object o = redisTemplate.opsForValue().get("hostGoods:" + category + ":" + optionOne + ":" + optionTwo);
        return o != null ? ResponseDTO.success(o) : ResponseDTO.error("查询错误");
    }
}

